package wang;

import com.rabbitmq.client.*;
import com.wang.Sys;

import java.io.IOException;

/**
 * 发布订阅模式（广播模式） 消费者
 */
public class ConsumerRabbitMQ2 {
    public static void main(String[] args) throws Exception{

        // 1.创建连接工厂
        ConnectionFactory cf = new ConnectionFactory();
        cf.setHost(Sys.HOST);
        cf.setPort(Sys.PORT);
        cf.setVirtualHost(Sys.VIRTUALHOST);
        cf.setUsername(Sys.USER_NAME);
        cf.setPassword(Sys.PASSWORD);

        // 2.创建连接
        Connection connection = cf.newConnection();

        // 3.创建通道
        Channel channel = connection.createChannel();

        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(new String(body));
            }
        };


        // 4.获取信息
        channel.basicConsume(Sys.QUEUE_NAME_2,true,consumer);

    }
}
